<?php


namespace App\Repositories\Admin\User;


use App\Models\Menu;
use App\Models\MenuGroup;
use App\Repositories\BaseRepository;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use SebastianBergmann\CodeCoverage\Report\Xml\Project;

class MenuRepository extends BaseRepository
{

    /**
     * @return Menu|Model
     */
    public function model()
    {
        return new Menu();
    }
    public function menuTree($menuGroupId,$where = [], $param = [],$pname='pid',$resetId =0){
        $model = $this->model();
        $where['menu_group_id'] = $menuGroupId;
        $parent_id = $param[$pname] ?? $resetId;
        $data = $this->model()->where($where)->get();
        if ($data) {
            return getTree($data->toArray(), $parent_id,$pname);
        } else {

            return [];
        }
    }

    /**
     *
     * @param $type
     * @param $gid
     * @return array|false
     */
    public function menuSelect($type, $gid)
    {
        switch ($type){
            case Menu::TYPE_SECOND_MENU:
                return $this->model()->where([
                    'menu_group_id'=>$gid,
                    'pid'=>0
                ])->get();
            case Menu::TYPE_THIRD_MENU:
                //树形结构
                $data = $this->model()->with([
                    'children'
                ])->where(['pid'=>0,'menu_group_id'=>$gid])->get();
                if($data){
                    $ary = $data->toArray();
                    foreach ($ary as &$v){
                        $v['disabled'] = true;
                    }

                    return $ary;
                }else{
                    return [];
                }
            default:
                return false;
        }
    }
    public function create($data)
    {
        $menu = $this->model();

        $created = DB::transaction(function ()use($data,$menu){
            $menu->label = $data['label'];
            $menu->type = $data['type'];
            $menu->is_auth = $data['is_auth']??false;
            $menu->route = $data['route'];
            $menu->status = $data['status']??false;
            $menu->sort = $data['sort']??99;
            $menu->pid = $data['pid']??0;
            $menu->icon = $data['icon']??"";
            $menu->menu_group_id = $data['menu_group_id'];
            //保存
            $created = $menu->save();
            if($created&&$menu->is_auth){
                if(isset($data['permissions'])&&$data['permissions']){
                    $menu->permissions()->sync($data['permissions']);
                    $menu->load('permissions');
                }
                if(isset($data['roles'])&&$data['roles']){
                    $menu->roles()->sync($data['roles']);
                    $menu->load('roles');
                }
            }
            return $created;
        });
        return $created?$menu:false;
    }

    public function allotPermission($permission, Menu $menu)
    {
        return $menu->permissions()->sync($permission);
    }
    public function parentMenu($type){
        $model = $this->model();
        $menu = [];
        switch ($type){
            case Menu::TYPE_LEFT_MENU:
                /**
                 * 查询顶级菜单
                 */
                $menu = $model->where(['pid'=>0])->select();
                break;
            case Menu::TYPE_CHILD_MENU:
                $menu = $model->where(['pid'=>0])->with(['children'])->select();
                break;
            default:
                return [];
        }
        foreach($menu as $key=>$val){
            $menu[$key]['is_disabled'] = 0;
            if($type==Menu::TYPE_CHILD_MENU){
                $menu[$key]['is_disabled'] = 1;
                if(isset($val['children'])){
                    foreach($val['children'] as $k=>$v){

                    }
                }
            }
        }
        return $menu;
    }
    /*
     * 菜单组
     */
    public function menuGroupModel(){
        return new MenuGroup();
    }
    public function menuGroups($status,$size){
        $builder  = $this->menuGroupModel();
        $page = $builder->when($status>-1,function ($query)use($status){
            $query->where('status',$status);
        })->paginate($size);
        return $page;
    }

    /**
     * @param $data
     * @return MenuGroup|false
     */
    public function createMenuGroup($data){
        $menu_group = $this->menuGroupModel();
        $menu_group->title = $data['title'];
        $menu_group->token = uuid('nav-');
        $menu_group->status = $data['status'];
        return $menu_group->save()?$menu_group:false;
    }
    public function updateMenuGroup(MenuGroup $menuGroup,$data){
        $menuGroup->title = $data['title'];
        $menuGroup->status = $data['status'];
        return $menuGroup->save()?$menuGroup->refresh():false;
    }



    public function menuGroupDelete(MenuGroup $menuGroup)
    {
        return DB::transaction(function ()use($menuGroup){
            $id = $menuGroup->id;
            $DELETE = $menuGroup->delete();
            if($DELETE){
                $menus = Menu::where('menu_group_id',$id)->count();
                if(!$menus){
                    return true;
                }
                return Menu::where('menu_group_id',$id)->delete();
            }
            return false;
        });
    }

}
